package vn.orderonline.web.admin.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import vn.orderonline.com.core.business.food.IFood;
import vn.orderonline.com.core.mvc.model.FoodDto;

@Controller
@RequestMapping("/food")
public class FoodController {

	private static final Logger LOGGER = Logger
			.getLogger(FoodController.class);

	@Autowired
	private IFood iFood;

	// @RequestMapping(value = "/formsearch", method = RequestMethod.GET)
	// public String createFormSearch(ModelMap model) {
	//
	// System.out.println("Start form search!");
	// Map<String,String> country = new LinkedHashMap<String,String>();
	// //Map<String,String> country = new HashMap<String,String>();
	// country.put("US", "United Stated");
	// country.put("CHINA", "China");
	// country.put("SG", "Singapore");
	// country.put("MY", "Malaysia");
	//
	// Map<String,String> javaSkill = new LinkedHashMap<String,String>();
	// //Map<String,String> javaSkill = new HashMap<String,String>();
	// javaSkill.put("Hibernate", "Hibernate adadf");
	// javaSkill.put("Spring", "Spring a");
	// javaSkill.put("Apache Wicket", "Apache Wicket b");
	// javaSkill.put("Struts", "Struts c");
	//
	// model.addAttribute("countryList", country);
	// model.addAttribute("javaSkillsList", javaSkill);
	// System.out.println("End form search!");
	//
	// return "/food/searchfood";
	// }

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String getListFood(@RequestParam Integer restaurantId,
			@RequestParam String restaurantName,
			@RequestParam Integer menuFoodId,
			@RequestParam String menuFoodName, ModelMap model) {
		LOGGER.info("START GET LIST FOOD!");
		FoodDto food = null;
		try {
			food = new FoodDto();
			food.setRestaurantId(restaurantId);
			food.setRestaurantName(restaurantName);
			food.setMenuFoodId(menuFoodId);
			food.setMenuFoodName(menuFoodName);
			model.addAttribute("food", food);
			model.addAttribute("list",
					iFood.getAllFood(restaurantId, menuFoodId));
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN GET LIST FOOD!", e);
		}
		LOGGER.info("END GET LIST FOOD!");
		return "/food/searchfood";
	}
	
	@RequestMapping(value = "/searchfood", method = RequestMethod.POST)
	public String searchFood(@ModelAttribute FoodDto food, ModelMap model) {
		LOGGER.info("START SEARCH FOOD!");
		try {
			model.addAttribute("food", food);
			model.addAttribute("list", iFood.searchFood(food));
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN SEACH FOOD!", e);
		}
		LOGGER.info("END SEARCH FOOD!");
		return "/food/searchfood";
	}

	@RequestMapping(value = "/addform", method = RequestMethod.GET)
	public String createFormAdd(@RequestParam Integer restaurantId,
			@RequestParam String restaurantName,
			@RequestParam Integer menuFoodId,
			@RequestParam String menuFoodName, ModelMap model) {
		LOGGER.info("START CREATE FORM ADD FOOD!");
		FoodDto food = null;
		try {
			food = new FoodDto();
			food.setRestaurantId(restaurantId);
			food.setRestaurantName(restaurantName);
			food.setMenuFoodId(menuFoodId);
			food.setMenuFoodName(menuFoodName);
			model.addAttribute("food", food);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN CREATE FORM ADD FOOD!");
		}
		LOGGER.info("END CREATE FORM ADD FOOD!");
		return "/food/addfood";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addFood(@ModelAttribute FoodDto food,
			RedirectAttributes redirectAttrs) {
		LOGGER.info("START ADD NEW FOOD!");
		try {
			iFood.addFood(food);
			redirectAttrs.addAttribute("restaurantId", food.getRestaurantId());
			redirectAttrs.addAttribute("restaurantName",
					food.getRestaurantName());
			redirectAttrs.addAttribute("menuFoodId", food.getMenuFoodId());
			redirectAttrs.addAttribute("menuFoodName", food.getMenuFoodName());
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN ADD NEW FOOD!", e);
		}
		LOGGER.info("END ADD NEW FOOD!");
		return "redirect:/food/list";
	}

	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public String detailFood(@RequestParam Integer restaurantId,
			@RequestParam Integer menuFoodId, @RequestParam Integer foodId,
			ModelMap model) {
		LOGGER.info("START VIEW DETAIL FOOD!");
		try {
			model.addAttribute("food",
					iFood.viewFood(foodId));
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN VIEW DETAIL FOOD!", e);
		}
		LOGGER.info("END VIEW DETAIL FOOD!");
		return "food/detailfood";
	}

	@RequestMapping(value = "/formupdate", method = RequestMethod.GET)
	public String createFormUpdate(@RequestParam Integer restaurantId,
			@RequestParam Integer menuFoodId, @RequestParam Integer foodId,
			ModelMap model) {
		LOGGER.info("START CREATE FORM UPDATE FOOD!");
		try {
			model.addAttribute("food",
					iFood.viewFood(foodId));
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN CREATE FORM UPDATE FOOD!");
		}
		LOGGER.info("END CREATE FORM UPDATE FOOD!");
		return "food/updatefood";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(@ModelAttribute FoodDto food,
			RedirectAttributes redirectAttrs) {
		LOGGER.info("START UPDATE FOOD!");
		try {
			iFood.updateFood(food);
			redirectAttrs.addAttribute("restaurantId", food.getRestaurantId());
			redirectAttrs.addAttribute("restaurantName",
					food.getRestaurantName());
			redirectAttrs.addAttribute("menuFoodId", food.getMenuFoodId());
			redirectAttrs.addAttribute("menuFoodName", food.getMenuFoodName());
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN UPDATE FOOD!", e);
		}
		LOGGER.info("END UPDATE FOOD!");
		return "redirect:/food/list";
	}

	@RequestMapping(value = "/delete")
	public String deleteFood(@RequestParam Integer restaurantId,
			@RequestParam String restaurantName,
			@RequestParam Integer menuFoodId,
			@RequestParam String menuFoodName, @RequestParam Integer foodId, RedirectAttributes redirectAttrs) {
		LOGGER.info("START DELETE FOOD!");
		try {
			iFood.deleteFood(menuFoodId, foodId);
			redirectAttrs.addAttribute("restaurantId", foodId);
			redirectAttrs.addAttribute("restaurantName", restaurantName);
			redirectAttrs.addAttribute("menuFoodId", menuFoodId);
			redirectAttrs.addAttribute("menuFoodName", menuFoodName);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN DELETE FOOD!", e);
		}
		LOGGER.info("END DELETE FOOD!");
		return "redirect:/food/list";
	}

	// @RequestMapping(value = "/delete", method = RequestMethod.POST)
	// @ResponseBody public String deleteFood(@RequestParam Integer menuFoodId,
	// @RequestParam Integer foodId) {
	// LOGGER.info("START DELETE FOOD!");
	// Boolean flag = true;
	// try {
	// iFood.deleteFood(menuFoodId, foodId);
	// } catch (Exception e) {
	// flag = false;
	// LOGGER.error("ERROR WHEN DELETE FOOD!", e);
	// }
	// LOGGER.info("END DELETE FOOD!");
	// return flag.toString();
	// }

	@RequestMapping(value = "/checkexistedfood", method = RequestMethod.GET)
	@ResponseBody
	public String checkExistedFood(@RequestParam Integer menuFoodId,
			@RequestParam String foodName) {
		LOGGER.info("START CHECK EXISTED FOOD NAME!");
		Boolean flag = null;
		try {
			flag = iFood.checkExistedFood(menuFoodId, foodName);
		} catch (Exception e) {
			LOGGER.error("ERROR CHECK EXISTED FOOD NAME!", e);
		}
		LOGGER.info("END CHECK EXISTED FOOD NAME!");
		return flag.toString();
	}

}
